package main

import "fmt"

func main() {

	s1 := []int{}
	fmt.Printf("len(s1) = %d, cap(s1) = %d\n", len(s1), cap(s1))
	fmt.Println("s1 = ", s1)

	// 是在原来切片的末尾添加元素
	s1 = append(s1, 1)
	s1 = append(s1, 2)
	s1 = append(s1, 3)

	fmt.Printf("len(s1) = %d, cap(s1) = %d\n", len(s1), cap(s1))
	fmt.Println("s1 = ", s1)

	s2 := []int{1, 2, 3}
	fmt.Printf("len(s2) = %d, cap(s2) = %d\n", len(s2), cap(s2))
	fmt.Println("s2 = ", s2)

	s2 = append(s2, 4)
	s2 = append(s2, 5)
	fmt.Printf("len(s2) = %d, cap(s2) = %d\n", len(s2), cap(s2))
	fmt.Println("s2 = ", s2)

	// 如果超过原来的容量, 通常以2倍容量扩容
	s := make([]int, 0, 1) // 容量为1
	oldCap := cap(s)

	for i := 0; i < 20; i++ {
		s = append(s, i)
		if newCap := cap(s); oldCap < newCap {
			fmt.Printf("oldCap = %d, newCap = %d\n", oldCap, newCap)
			oldCap = newCap
		}
	}
}
